<?php

namespace Admin\Controller;

class ActionController extends AdminCoreController{
	
    public $Model = null;

    protected function _initialize(){
        parent::_initialize();
        $this->Model = D('Action');
    }
	
	public function index(){
		$module = I('get.module');	
		if(IS_AJAX){
			$mark = I('get.mark');
			$search = I('get.search');
			if($mark){
                $map[$mark] = array('like', "$search%");
			}
			$map['module'] = $module;
			$page_size = 15;
			$count = $this->Model->where($map)->count();
			$Page = new \Think\Page($count, $page_size);
			$list = $this->Model
				->where($map)
				->order("id desc")
				->limit($Page->firstRow . ',' . $Page->listRows)
				->select();
			foreach($list as $key=>$value){
				if($list[$key]['type'] == 1){
					$list[$key]['type'] = "系统";
				}elseif($list[$key]['type'] == 2){
					$list[$key]['type'] = "用户";
				}elseif($list[$key]['status'] == 1){
					$list[$key]['status'] = "开启";
				}elseif($list[$key]['status'] == 2){
					$list[$key]['status'] = "关闭";
				}
			}
			$str = $this->tojson($list, $count);			
			echo $str;
			exit;
			
        }else{
			/*$add = check_rule('Admin/Action/add','Admin', session('userinfo.id'));
			$this->assign('add', $add);
			$edit = check_rule('Admin/Action/edit','Admin', session('userinfo.id'));
			$this->assign('edit', $edit);
			$del = check_rule('Admin/Action/del','Admin', session('userinfo.id'));
			$this->assign('del', $del);
			$search = check_rule('Admin/Action/search','Admin', session('userinfo.id'));
			$this->assign('search', $search);*/
			$this->assign('module', $module);
            $this->display();
        }
	}
	
	public function add(){
		if(IS_POST){
			$post_data = I('post.');
			$data = $this->Model->create($post_data);
			if($data){
				$result = $this->Model->update($data);
				if($result){
					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
				}else{
					$error = $this->Model->getError();
					echo json_encode(array('info'=>$error, 'status'=>'n'));
				}
			}else{
				$error = $this->Model->getError();
				echo json_encode(array('info'=>$error, 'status'=>'n'));
			}			
		}else{
			$module = I('get.module');
			$this->assign('module', $module);
			$this->display ();
		}
	}
	
	public function edit(){
		if(IS_POST){
			$post_data = I('post.');
			$data = $this->Model->create($post_data);
			if($data){
				$result = $this->Model->update($data);
				if($result){
					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
				}else{
					$error = $this->Model->getError();
					echo json_encode(array('info'=>$error, 'status'=>'n'));
				}
			}else{
				$error = $this->Model->getError();
				echo json_encode(array('info'=>$error, 'status'=>'n'));
			}
		}else{
			$module = I('get.module');
			$this->assign('module', $module);
			$_info = I('get.');
			$info = $this->Model->where(array('id' => $_info ['id']))->find();
			$this->assign('info', $info);
			$this->display();
		}
	}
	
	public function del(){
		$ids = I('get.ids');
		$map['id'] = array('in', $ids);
		$res = $this->Model->where($map)->delete();
		if($res){
            echo json_encode(array('info'=>'删除成功', 'status'=>'y'));
        }else{
            $error = $this->Model->getError();
			echo json_encode(array('info'=>$error, 'status'=>'n'));
        }
	}
	
}